package com.renyi.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.renyi.http.HttpClient;
import com.renyi.http.HttpExpressResult;
import com.renyi.http.HttpExpressResultKM;
import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.http.Response;
import com.renyi.model.MybatisServiceDoctor;
import com.renyi.util.CalendarUtil;
import com.renyi.util.ConfigUtil;
import com.renyi.util.JSONUtils;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class SystemController extends BaseController {

	@Autowired
	private MybatisServiceDoctor mybatisServiceDoctor;

	private Logger log = LogManager.getLogger(this.getClass().getName());

	private static List<String> suffixAry = new ArrayList<String>();

	static {
		// System.out.println("-------初始化图片格------");
		if (suffixAry.size() == 0) {
			suffixAry.add("jpg");
			suffixAry.add("png");
			suffixAry.add("gif");
			suffixAry.add("jpeg");
		}

	}

	// 患者登陆
	@RequestMapping("/getLoginPatient")
	public void getLoginPatient(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();

			String telephone = map.get("telephone").toString().trim();
			String password = map.get("password").toString().trim();

			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("telephone", telephone);
			paramMap.put("password", password);
			paramMap.put("userTypeID", 1);

			long isExist = this.mybatisServiceDoctor.findIbatisListCount("getPatientIDSQL", paramMap);
			if (isExist > 0) {
				List<HashMap<String, Object>> data = this.mybatisServiceDoctor.findMybatisList("getLoginPatientSQL",
						paramMap);

				Map<String, Object> dataMap = new HashMap<String, Object>();
				if (data != null && data.size() > 0) {
					Map<String, Object> mapId = data.get(0);
					dataMap.put("uid", mapId.get("uid"));
					dataMap.put("userName", mapId.get("userName"));
					dataMap.put("photoUrl", mapId.get("photoUrl"));
					dataMap.put("chatQQ", mapId.get("chatQQ"));
					dataMap.put("chatPassword", mapId.get("password"));
					dataMap.put("userTypeID", 1);
				}

				if (data.size() > 0) {
					int key = (Integer) this.mybatisServiceDoctor.insertIbatisObject("insertLoginSQL", dataMap);
					int tkey = (Integer) this.mybatisServiceDoctor.updateIbatisObject("updateLogonTimeSQL", paramMap);
					result.setCode(0);
					result.setMsg("");
					result.setData(dataMap);
				} else {
					result.setCode(2);
					result.setMsg("密码错误");
				}
			} else {
				result.setCode(1);
				result.setMsg("用户不存在");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}
	
	// 患者注册
	@RequestMapping("/setRegisterPatient")
	public void setRegisterPatient(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();

			String telephone = map.get("telephone").toString().trim();
			String password = map.get("password").toString().trim();
			String userName = map.get("userName") != null ? map.get("userName").toString() : "";
			String refereeQQ = map.get("refereeQQ") != null ? map.get("refereeQQ").toString() : "";

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("telephone", telephone);
			pMap.put("password", password);
			pMap.put("userName", userName);
			pMap.put("chatQQ", telephone + "1");
			pMap.put("refereeQQ", refereeQQ);
			long isExist = this.mybatisServiceDoctor.findIbatisListCount("getPatientIDSQL", pMap);
			if (isExist < 1) {
				// 0 成功 -1 失败
				List<HashMap<String, Object>> listRes = this.mybatisServiceDoctor
						.procedureMybatisList("setRegisterPatientSQL", pMap);
				String resType = "";
				if (listRes.size() > 0) {
					resType = listRes.get(0).get("resType") != null ? listRes.get(0).get("resType").toString() : "0";
					if (resType.equals("0")) {
						result.setCode(0);
						result.setMsg("");
					} else {
						result.setCode(-1);
						result.setMsg("失败");
					}

				}
			} else {
				result.setCode(1);
				result.setMsg("手机号已注册");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 医生登陆
	@RequestMapping("/getLoginDoctor")
	public void getLoginDoctor(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();

			String telephone = map.get("telephone").toString().trim();
			String password = map.get("password").toString().trim();

			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("telephone", telephone);
			paramMap.put("password", password);
			paramMap.put("userTypeID", 2);
			long isExist = this.mybatisServiceDoctor.findIbatisListCount("getDoctorIDSQL", paramMap);
			if (isExist > 0) {
				List<HashMap<String, Object>> data = this.mybatisServiceDoctor.findMybatisList("getLoginDoctorSQL",
						paramMap);

				Map<String, Object> dataMap = new HashMap<String, Object>();
				Map<String, Object> mapId = null;
				if (data != null && data.size() > 0) {
					mapId = data.get(0);
					mapId.put("userTypeID", 2);
					// if (mapId.get("isCheck").toString().trim().equals("0")) {
					// result.setCode(3);
					// result.setMsg("正在审核,请稍后");
					// } else {
					int key = (Integer) this.mybatisServiceDoctor.insertIbatisObject("insertLoginSQL", mapId);
					int tkey = (Integer) this.mybatisServiceDoctor.updateIbatisObject("updateLogonTimeSQL", paramMap);
					result.setCode(0);
					result.setMsg("");
					result.setData(mapId);
					// }
				} else {
					result.setCode(2);
					result.setMsg("密码错误");
				}

			} else {
				result.setCode(1);
				result.setMsg("用户不存在");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 医生注册
	@RequestMapping("/setRegisterDoctor")
	public void setRegisterDoctor(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();

			String telephone = paramMap.get("telephone") != null ? paramMap.get("telephone").toString() : "";
			String password = paramMap.get("password") != null ? paramMap.get("password").toString() : "";
			String userName = paramMap.get("userName") != null ? paramMap.get("userName").toString() : "";
			String refereeQQ = paramMap.get("refereeQQ") != null ? paramMap.get("refereeQQ").toString() : "";
			String hospitalID = paramMap.get("hospitalID") != null ? paramMap.get("hospitalID").toString() : "";
			String technicalID = paramMap.get("technicalID") != null ? paramMap.get("technicalID").toString() : "";
			String departmentID = paramMap.get("departmentID") != null ? paramMap.get("departmentID").toString() : "";
			String speciality = paramMap.get("speciality") != null ? paramMap.get("speciality").toString() : "";
			// 图片地址--头像、身份证正反面、两个执照
			String photoUrl = paramMap.get("photoUrl") != null ? paramMap.get("photoUrl").toString() : "";
			String creFirstUrl = paramMap.get("creFirstUrl") != null ? paramMap.get("creFirstUrl").toString() : "";
			String creSecondUrl = paramMap.get("creSecondUrl") != null ? paramMap.get("creSecondUrl").toString() : "";
			String carFrontUrl = paramMap.get("carFrontUrl") != null ? paramMap.get("carFrontUrl").toString() : "";
			String carBackUrl = paramMap.get("carBackUrl") != null ? paramMap.get("carBackUrl").toString() : "";
			int sex = paramMap.get("sex") != null ? Integer.valueOf(paramMap.get("sex").toString()) : 0;

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("telephone", telephone);
			pMap.put("password", password);
			pMap.put("userName", userName);
			pMap.put("refereeQQ", refereeQQ);
			pMap.put("hospitalID", hospitalID);
			pMap.put("technicalID", technicalID);
			pMap.put("departmentID", departmentID);
			pMap.put("speciality", speciality);
			// 图片地址--头像、身份证正反面、两个执照
			pMap.put("photoUrl", photoUrl);
			pMap.put("creFirstUrl", creFirstUrl);
			pMap.put("creSecondUrl", creSecondUrl);
			pMap.put("carFrontUrl", carFrontUrl);
			pMap.put("carBackUrl", carBackUrl);
			pMap.put("sex", sex);
			long isExist = this.mybatisServiceDoctor.findIbatisListCount("getDoctorIDSQL", paramMap);
			if (isExist < 1) {
				// 0 成功 -1 失败
				List<HashMap<String, Object>> listRes = this.mybatisServiceDoctor
						.procedureMybatisList("setRegisterDoctorSQL", pMap);
				String resType = "";
				if (listRes.size() > 0) {
					resType = listRes.get(0).get("resType") != null ? listRes.get(0).get("resType").toString() : "0";
					if (resType.equals("0")) {
						result.setCode(0);
						result.setMsg("");
					} else {
						result.setCode(-1);
						result.setMsg("失败");
					}

				}
			} else {
				result.setCode(1);
				result.setMsg("手机号已注册");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 修改密码
	@RequestMapping("modifyPassword")
	public void modifyPassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String telephone = paramMap.get("telephone") != null ? paramMap.get("telephone").toString() : "";
			String password = paramMap.get("password") != null ? paramMap.get("password").toString() : "";
			String userType = paramMap.get("userType") != null ? paramMap.get("userType").toString() : "";

			if ("".equals(telephone) || "".equals(password) || "".equals(userType)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("telephone", telephone);
			pMap.put("password", password);
			pMap.put("userType", userType);
			int key = this.mybatisServiceDoctor.updateIbatisObject("modifyPasswordSQL", pMap);
			if (key > 0) {
				result.setCode(0); // 0：成功
				result.setMsg("");
			} else {
				result.setCode(-1); // 0：成功
				result.setMsg("");
			}
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 图片上传
	@RequestMapping("/imageSave")
	public void imageSave(HttpServletRequest request, HttpServletResponse response) {
		HttpResult result = new HttpResult();
		result.setCode(-1);
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成�?
				this.ajaxResultJson(response, result);
				return;
			}

			String imgBase64 = getString(request, "data") != null ? getString(request, "data") : "";// 图片base64
			if ("".equals(imgBase64)) {
				result.setMsg("图片内容为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> postData = new HashMap<String, Object>();
			postData.put("data", imgBase64);
			postData.put("jsonParam", super.getJsonParam(request));
			Response res = HttpClient.post(ConfigUtil.getImgUploadURL(), postData);
			String imgPathJson = res.asString();

			result = JSONUtils.fromJson(imgPathJson, HttpResult.class);
			if (result == null) {
				log.error("结果解析异常:" + imgPathJson);
				result = new HttpResult();
				result.setCode(-1);
				result.setMsg("储存图片异常");
			}

		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("储存图片异常");
		}

		this.ajaxResultJson(response, result);
		return;
	}

	// 头像上传
	@RequestMapping("/uploadPhoto")
	public void uploadPhoto(HttpServletRequest request, HttpServletResponse response) {
		HttpResult result = new HttpResult();
		result.setCode(-1);
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成�?
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			String userType = paramMap.get("userType") != null ? paramMap.get("userType").toString() : "";
			if ("".equals(userType)) {
				result.setMsg("用户类型为空");
				this.ajaxResultJson(response, result);
				return;
			}

			String imgBase64 = getString(request) != null ? getString(request) : "";// 图片base64
			if ("".equals(imgBase64)) {
				result.setMsg("图片内容为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> postData = new HashMap<String, Object>();
			postData.put("data", imgBase64);
			postData.put("jsonParam", super.getJsonParam(request));
			Response res = HttpClient.post(ConfigUtil.getImgUploadURL(), postData);
			String imgPathJson = res.asString();

			result = JSONUtils.fromJson(imgPathJson, HttpResult.class);
			if (result == null) {
				log.error("结果解析异常:" + imgPathJson);
				result = new HttpResult();
				result.setCode(-1);
				result.setMsg("储存图片异常");
			}
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if (!"".equals(uid)) {
				HashMap<String, Object> pMap = new HashMap<String, Object>();
				pMap.put("photoUrl", result.getData());
				pMap.put("uid", uid);
				int key = 0;
				if (userType.equals("1")) {
					key = this.mybatisServiceDoctor.updateIbatisObject("uploadPatientPhotoSQL", pMap);
				} else {
					key = this.mybatisServiceDoctor.updateIbatisObject("uploadDoctorPhotoSQL", pMap);
				}

				if (key < 1) {
					log.error("结果解析异常:" + imgPathJson);
					result = new HttpResult();
					result.setCode(-1);
					result.setMsg("储存图片异常");
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("储存图片异常");
		}

		this.ajaxResultJson(response, result);
		return;
	}

	// 反馈意见
	@RequestMapping("/setFeedbackInfo")
	public void setFeedbackInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String userType = paramMap.get("userType") != null ? paramMap.get("userType").toString() : "";
			String message = paramMap.get("message") != null ? paramMap.get("message").toString() : "";
			String telephone = paramMap.get("telephone") != null ? paramMap.get("telephone").toString() : "";
			String imgUrl = paramMap.get("imgUrl") != null ? paramMap.get("imgUrl").toString() : "";

			if ("".equals(userType) || "".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("userType", userType);
			pMap.put("message", message);
			pMap.put("telephone", telephone);
			pMap.put("imgUrl", imgUrl);
			int key = (Integer) this.mybatisServiceDoctor.insertIbatisObject("setFeedbackInfoSQL", pMap);
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 查看快递信息
	@RequestMapping("/getExpressInfo")
	public void getExpressInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = paramMap.get("orderID") != null ? paramMap.get("orderID").toString() : "";

			if ("".equals(orderID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("订单号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);

			List<HashMap<String, Object>> data = this.mybatisServiceDoctor.findMybatisList("getExpressInfoSQL", pMap);

			Map<String, Object> dataMap = new HashMap<String, Object>();
			result.setCode(-1);
			result.setMsg("");
			if (data != null && data.size() > 0) {
				if (orderID.contains("K")) {
					try {// "KM16080301477"
						String url = String.format(
								"http://drugstore.km1818.com:8182/zyf/web?methodName=getWLInfoNoValicode&order_id=%s",
								data.get(0).get("expressNumber").toString().trim());
						String res = HttpClient.getAddressStr(url);
						HttpExpressResultKM her = JSONUtils.fromJson(res, HttpExpressResultKM.class);
						Map<String, Object> map = new HashMap<String, Object>();
						List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
						if (her.getHead().get("code").equals("0")) {
							HttpExpressResultKM.ExpressKM ekm = JSONUtils.fromJson(her.getData(),
									HttpExpressResultKM.ExpressKM.class);
							map.put("company", ekm.getCompany().get("logis_company").toString());
							map.put("expressNumber", ekm.getCompany().get("order_id").toString());
							for (int i = 0; i < ekm.getList().size(); i++) {
								HashMap<String, Object> em = new HashMap<String, Object>();
								em.put("context", ekm.getList().get(i).get("addr_info").toString());
								em.put("time", ekm.getList().get(i).get("current_lo_time").toString());
								list.add(em);
							}
							map.put("express", list);
							result.setCode(0);
							result.setData(list);

						} else {
							result.setCode(-1);
							result.setMsg("");
						}
					} catch (Exception e) {
						// TODO: handle exception
					}
				} else {
					try {
						String url = String.format(
								"http://apis.haoservice.com/lifeservice/exp?com=%s&no=%s&order=desc&key=9826e5df7fbc446898ca4491e4dddc93",
								data.get(0).get("alias").toString().trim(),
								data.get(0).get("expressNumber").toString().trim());
						String res = HttpClient.getAddressStr(url);
						HttpExpressResult her = JSONUtils.fromJson(res, HttpExpressResult.class);
						String resLog = "";
						switch (her.getError_code()) {
						case 0:
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("company", her.getResult().get("company").toString());
							map.put("expressNumber", her.getResult().get("no").toString());
							map.put("express", her.getResult().get("data"));
							result.setCode(0);
							result.setMsg("");
							result.setData(map);
							break;
						case 10003:
							resLog = "KEY过期";
							break;
						case 10012:
							resLog = "当前Key请求超过限制";
							break;
						case 10020:
							resLog = "接口维护";
							break;
						case 10021:
							resLog = "接口停用";
							break;
						case 10022:
							resLog = "appKey按需剩余请求次数为零";
							break;
						case 10025:
							resLog = "没有查询到结果";
							break;
						case 201802:
							resLog = "查询无结果";
							break;

						default:
							break;
						}
						if (!"".equals(resLog)) {
							log.debug(resLog);
						}
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			} else {
				result.setCode(-1);
				result.setMsg("");
			}
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 查看康美快递信息
	@RequestMapping("/getExpressInfoKM")
	public void getExpressInfoKM(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = paramMap.get("orderID") != null ? paramMap.get("orderID").toString() : "";

			if ("".equals(orderID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("订单号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);

			List<HashMap<String, Object>> data = this.mybatisServiceDoctor.findMybatisList("getExpressInfoSQL", pMap);

			Map<String, Object> dataMap = new HashMap<String, Object>();
			result.setCode(-1);
			result.setMsg("");
			if (data != null && data.size() > 0) {
				try {// "KM16080301477"
					String url = String.format(
							"http://drugstore.km1818.com:8182/zyf/web?methodName=getWLInfoNoValicode&order_id=%s",
							data.get(0).get("expressNumber").toString().trim());
					String res = HttpClient.getAddressStr(url);
					HttpExpressResultKM her = JSONUtils.fromJson(res, HttpExpressResultKM.class);
					Map<String, Object> map = new HashMap<String, Object>();
					List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
					if (her.getHead().get("code").equals("0")) {
						HttpExpressResultKM.ExpressKM ekm = JSONUtils.fromJson(her.getData(),
								HttpExpressResultKM.ExpressKM.class);
						map.put("company", ekm.getCompany().get("logis_company").toString());
						map.put("expressNumber", ekm.getCompany().get("order_id").toString());
						for (int i = 0; i < ekm.getList().size(); i++) {
							HashMap<String, Object> em = new HashMap<String, Object>();
							em.put("context", ekm.getList().get(i).get("addr_info").toString());
							em.put("time", ekm.getList().get(i).get("current_lo_time").toString());
							list.add(em);
						}
						map.put("express", list);
						result.setCode(0);
						result.setData(list);

					} else {
						result.setCode(-1);
						result.setMsg("");
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			} else {
				result.setCode(-1);
				result.setMsg("");
			}
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 发送短信
	@RequestMapping("/sendSMS")
	public void sendSMS(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String tel = paramMap.get("tel") != null ? paramMap.get("tel").toString() : "";
			String text = paramMap.get("text") != null ? paramMap.get("text").toString() : "";
			String taskName = paramMap.get("taskName") != null ? paramMap.get("taskName").toString() : "验证码验证";
			if ("".equals(tel) || "".equals(text)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			String uriAPI = "http://121.52.209.124:8888/sms.aspx?action=send&userid=6277&account=a10446&password=123456&mobile="
					+ tel + "&content=【仁医】" + text + "&sendTime=&taskName=" + taskName
					+ "&checkcontent=1&mobilenumber=1&countnumber=1&telephonenumber=0";

			/* 建立HTTP Post连线 */
			HttpPost httpRequest = new HttpPost(uriAPI);
			// Post运作传送变数必须用NameValuePair[]阵列储存
			// 传参数 服务端获取的方法为request.getParameter("name")
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("name", "this is post"));
			try {
				// 发出HTTP request
				httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
				// 取得HTTP response
				HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);

				// 若状态码为200 ok
				if (httpResponse.getStatusLine().getStatusCode() == 200) {
					// 取出回应字串
					// String
					// strResult=EntityUtils.toString(httpResponse.getEntity());
					result.setCode(0);
				} else {
					result.setCode(-1);
				}
			} catch (ClientProtocolException e) {
				result.setCode(-1);
			} catch (UnsupportedEncodingException e) {
				result.setCode(-1);
			} catch (IOException e) {
				result.setCode(-1);
			}
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 版本升级
	@RequestMapping("/getVersion")
	public void getVersion(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 1仁医 2仁医医生
			int type = paramMap.get("type") != null ? Integer.valueOf(paramMap.get("type").toString()) : 1;
			Map<String, Object> pMap = new HashMap<String, Object>();
			if (type == 1) {
				pMap.put("version", ConfigUtil.getRyAppVersion());
				pMap.put("url", ConfigUtil.getRyAppappUpdateURL());
			} else if (type == 2) {
				pMap.put("version", ConfigUtil.getRyysAppVersion());
				pMap.put("url", ConfigUtil.getRyysAppappUpdateURL());
			}

			result.setCode(0);
			result.setData(pMap);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 帮助
	@RequestMapping("getHelpList")
	public void getHelpList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			int type = (int) (paramMap.get("type") != null
					? Math.floor(Integer.valueOf(paramMap.get("type").toString())) : 1);

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("type", type);
			List<HashMap<String, Object>> data = this.mybatisServiceDoctor.findMybatisList("getHelpListSQL", pMap);
			if (data != null) {
				result.setCode(0); // 0：成功
				result.setData(data);
			} else {
				result.setCode(-1);
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 关于
	@RequestMapping("getAbout")
	public void getAbout(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			int type = (int) (paramMap.get("type") != null
					? Math.floor(Integer.valueOf(paramMap.get("type").toString())) : 1);

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("type", type);
			List<HashMap<String, Object>> data = this.mybatisServiceDoctor.findMybatisList("getAboutSQL", pMap);
			if (data != null) {
				result.setCode(0); // 0：成功
				result.setData(data);
			} else {
				result.setCode(-1);
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生获取经营信息
	@RequestMapping("getManageClinic")
	public void getManageClinic(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			int type = (int) (paramMap.get("type") != null
					? Math.floor(Integer.valueOf(paramMap.get("type").toString())) : 1);

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("type", type);
			List<HashMap<String, Object>> data = this.mybatisServiceDoctor.findMybatisList("getManageClinicSQL", pMap);
			if (data != null) {
				result.setCode(0); // 0：成功
				result.setData(data);
			} else {
				result.setCode(-1);
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 获取患者或者医生的手机和环信号密码（默认888888）,用于激活
	@RequestMapping("getTelPw")
	public void getTelPw(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			int type = paramMap.get("type") != null ? Integer.parseInt(paramMap.get("type").toString()) : 1;
			String telephone = paramMap.get("telephone") != null ? paramMap.get("telephone").toString().trim() : "";

			// if ("".equals(uid)) {
			// result.setCode(-1); // 0：成功
			// result.setMsg("用戶ID参数为空");
			// this.ajaxResultJson(response, result);
			// return;
			// }

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("type", type);// 1-患者APP 2-医生APP
			pMap.put("telephone", telephone);
			long isExist = 0;
			if (type == 1) {
				isExist = this.mybatisServiceDoctor.findIbatisListCount("getTelPwPatientSQL", pMap);
			} else {
				isExist = this.mybatisServiceDoctor.findIbatisListCount("getTelPwDoctorSQL", pMap);
			}
			if (isExist > 0) {
				result.setCode(0); // 0：成功
			} else {
				result.setCode(-1);
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}
}
